.text
.section .rodata
.LC0:
    .string "Input up to %d lines, and I will sort them.\n"
.LC1:
    .string "To stop, press the Enter key at a line''s start."
.LC2:
    .string "\nHere''s the sorted list:"

.text
.globl main
.type main, @function
main:
    pushq %rbp
    movq %rsp, %rbp

    // 需要多大栈空间呢？
    // sizeof(input) = 1620
    // 20 * sizeof(char*) = 160
    // sizeof(ct) = 4
    // sizeof(k) = 4
    // 加起来=1788，多预留点吧
    subq $1808, %rsp

    // 又到了分配栈空间的时间
    // input: 起始地址-1620(%rbp)
    // ptstr: 起始地址-1780(%rbp)
    // ct: -1784(%rbp)
    // k: -1788(%rbp)
    movl $0, -1784(%rbp) /* ct */
    movl $20, %esi
    leaq .LC0(%rip), %rdi
    movl $0, %eax
    call printf

    leaq .LC1(%rip), %rdi
    call puts

    jmp .WHILE_16_COMPARE
.WHILE_16_ENTER:
    movl -1784(%rbp) /* ct */, %eax
    cltq
    movq $81 /* SIZE */, %rcx
    mul %rcx
    leaq -1620(%rbp, %rax, 1), %rdx
    movl -1784(%rbp) /* ct */, %eax
    cltq
    movq %rdx, -1780(%rbp, %rax, 8)
    incl -1784(%rbp) /* ct */
.WHILE_16_COMPARE:
    cmpl $20 /* LIM */, -1784(%rbp) /* ct */
    jge .WHILE_16_COMPLETE
    movl -1784(%rbp) /* ct */, %eax
    cltq
    movq $81 /* SIZE */, %rcx
    mul %rcx
    leaq -1620(%rbp, %rax, 1), %rdi
    movl $81 /* SIZE */, %esi
    call s_gets
    cmpq $0, %rax
    je .WHILE_16_COMPLETE
    movl -1784(%rbp) /* ct */, %eax
    cltq
    movq $81 /* SIZE */, %rcx
    mul %rcx
    movb -1620(%rbp, %rax, 1), %al
    cmpb $0, %al
    je .WHILE_16_COMPLETE
    jmp .WHILE_16_ENTER

.WHILE_16_COMPLETE:
    leaq -1780(%rbp), %rdi
    movl -1784(%rbp) /* ct */, %esi
    call stsrt

    leaq .LC2(%rip), %rdi
    call puts

    movl $0, -1788(%rbp) /* k */
    jmp .FOR_23_COMPARE
.FOR_23_ENTER:
    movl -1788(%rbp) /* k */, %eax
    cltq
    movq -1780(%rbp, %rax, 8), %rdi
    call puts
.FOR_23_INCR:
    incl -1788(%rbp) /* k */
.FOR_23_COMPARE:
    movl -1788(%rbp) /* k */, %eax
    cmpl -1784(%rbp) /* ct */, %eax
    jl .FOR_23_ENTER

    movl $0, %eax
    movq %rbp, %rsp
    popq %rbp
    ret

.text
.globl stsrt
.type stsrt, @function
stsrt:
    pushq %rbp
    movq %rsp, %rbp

    subq $32, %rsp
    // strlist
    movq %rdi, -8(%rbp)
    // num
    movl %esi, -12(%rbp)
    // tmp, -20(%rbp)
    // i: -24(%rbp)
    // j: -28(%rbp)

    movl $0, -24(%rbp) /* i */
    jmp .FOR_33_COMPARE
.FOR_33_ENTER:
    movl -24(%rbp) /* i */, %eax
    leal 1(%eax), %eax
    movl %eax, -28(%rbp) /* j */
    jmp .FOR_35_COMPARE
.FOR_35_ENTER:
    movl -24(%rbp) /* i */, %eax
    cltq
    movq -8(%rbp) /* strlist */, %rdx
    movq (%rdx, %rax, 8), %rdi
    movl -28(%rbp) /* j */, %eax
    cltq
    movq (%rdx, %rax, 8), %rsi
    call strcmp
    cmpl $0, %eax
    jle .FOR_35_INCR
    // 交换指针
.FOR_35_CHANGE_POINTER:
    movl -24(%rbp) /* i */, %eax
    cltq
    movq -8(%rbp) /* strlist */, %rdx
    movq (%rdx, %rax, 8), %rax
    movq %rax, -20(%rbp) /* tmp */
    movl -28(%rbp) /* j */, %eax
    cltq
    movq (%rdx, %rax, 8), %rcx
    movl -24(%rbp) /* i */, %eax
    cltq
    movq %rcx, (%rdx, %rax, 8)
    movq -20(%rbp) /* tmp */, %rcx
    movl -28(%rbp) /* j */, %eax
    cltq
    movq %rcx, (%rdx, %rax, 8)
.FOR_35_INCR:
    incl -28(%rbp) /* j */
.FOR_35_COMPARE:
    movl -28(%rbp) /* j */, %eax
    cmpl -12(%rbp) /* num */, %eax
    jl .FOR_35_ENTER
.FOR_33_INCR:
    incl -24(%rbp) /* i */
.FOR_33_COMPARE:
    movl -24(%rbp) /* i */, %eax
    cmpl -12(%rbp) /* num */, %eax
    jl .FOR_33_ENTER

    movq %rbp, %rsp
    popq %rbp
    ret

.text
.globl s_gets
.type s_gets, @function
s_gets:
    pushq %rbp
    movq %rsp, %rbp

    // 得琢磨一下减多少
    // sizeof(st) + sizeof(n) + sizeof(ret_val) + sizeof(i) = 8 + 4 + 8 + 4 = 24
    subq $32, %rsp

    // 分配局部变量
    // st: -8(%rbp)
    // n: -12(%rbp)
    // ret_val: -20(%rbp)
    // i: -24(%rbp)
    movq %rdi, -8(%rbp) /* st */
    movl %esi, -12(%rbp) /* n */
    movl $0, -24(%rbp) /* i */
    // 这两句实际是没必要的
    movq -8(%rbp) /* st */, %rdi
    movl -12(%rbp) /* n */, %esi
    movq stdin(%rip), %rdx
    call fgets
    movq %rax, -20(%rbp) /* ret_val */

    cmpq $0, %rax
    je .EXIT
    jmp .WHILE_15_COMPARE
.WHILE_15_ENTER:
    incl -24(%rbp) /* i */
.WHILE_15_COMPARE:
    movq -8(%rbp) /* st */, %rdi
    movl -24(%rbp) /* i */, %eax
    cltq
    movb (%rdi, %rax, 1), %al
    cmpb $10, %al
    je .WHILE_15_BREAK
    cmpb $0, %al
    je .WHILE_15_BREAK
    jmp .WHILE_15_ENTER
.WHILE_15_BREAK:
    movq -8(%rbp) /* st */, %rdi
    movl -24(%rbp) /* i */, %eax
    cltq
    movb (%rdi, %rax, 1), %al
    cmpb $10, %al
    jne .WHILE_20_COMPARE
    movl -24(%rbp) /* i */, %eax
    cltq
    movb $0, (%rdi, %rax, 1)
    jmp .EXIT

.WHILE_20_COMPARE:
    call getchar
    cmpb $10, %al
    jne .WHILE_20_COMPARE
.EXIT:
    movq -20(%rbp) /* ret_val */, %rax
    movq %rbp, %rsp
    popq %rbp
    ret